//Jquery extensions for login function
$.extend({
	initTextField: function(id, label) {
		var field = $("#" + id);
		if ($.trim(field.attr("value")) == "") {
			field.addClass("empty-text-field");
			field.attr("value",label);
		}
		field.focus(function() {
			field.removeClass("empty-text-field");
			if ($.trim(field.attr("value")) == label) {
				field.attr("value","");
			}
		});
		field.blur(function() {
			if ($.trim(field.attr("value")) == "") {
				field.addClass("empty-text-field");
				field.attr("value",label);
			}
		});
	},
	initPasswordField: function(id, proxyId, label) {
		var field = $("#" + id);
		var proxyField = $("#" + proxyId);
		field.addClass("empty-text-field");
		field.attr("value",label);
		if ($.trim(proxyField.attr("value")) == "") {
			field.show();
			proxyField.hide();
		}
		field.focus(function() {
			field.hide();
			proxyField.show();
			proxyField.focus();
		});
		proxyField.blur(function() {
			if ($.trim(proxyField.attr("value")) == "") {
				field.show();
				proxyField.attr("value","");
				proxyField.hide();
			}
		});
	},
	showError: function(msg) {
		$("#msg-text").html(msg)
						.removeClass("success")
						.addClass("error");
		$("#message").fadeIn();
	},
	showSuccess: function(msg) {
		$("#msg-text").html(msg)
						.removeClass("error")
						.addClass("success");
		$("#message").fadeIn();
	},
	closeMessage: function() {
		$("#message").fadeOut();
	}
});
$(document).ready(function() {
	$.initTextField("username","Enter username");
	$.initPasswordField("password","password-proxy","Enter password");
	$("#login-btn").click(function() {
		var ret = true;
		var msg = "";
		if ($("#username").attr("value") == "Enter username") {
			ret = false;
			msg+="Username";
		}
		if ($("#password-proxy").attr("value") == "") {
			ret = false;
			if (msg.length > 0) msg+= " and ";
			msg+="Password";
		}
		if (msg.length > 0) {
			msg+= " cannot be empty";
			$.showError(msg);
			return;
		}
		$.ajaxLogin($("#username").attr("value"),$("#password-proxy").attr("value"));
	});
});